﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_mpaplication : System.Web.UI.Page
{
    rUser ruser;
    DataSet permisos;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }

        restricted();
        permiso();
        //Si es la primera vez que entra
        if (!Page.IsPostBack)
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getOpcionApp(1);
            
            
            if (!rxml.Equals("") || rxml != null)
            {
                permisos = Utility.getOpcionesbyApp(rxml);
                Session["pagespermit"] = permisos;
            }

            ruser = (rUser)Session["userid"];
            
            pintarOpciones();
        }
    }
    private void pintarOpciones()
    {
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();

        string pagop = service.getAppPages(1);
        DataSet pagopp = Utility.getPermitPage(pagop);

        Table table = new Table();
        table.CellPadding = 0;
        table.CellSpacing = 0;
        table.Width = 250;
        TableHeaderRow thr = new TableHeaderRow();
        TableHeaderCell thc1 = new TableHeaderCell();
        thc1.Text = "Option";
        thc1.Width = 180;
        thc1.BackColor = System.Drawing.Color.FromName("#9AC9F8");
        TableHeaderCell thc2 = new TableHeaderCell();
        thc2.Text = "Edit";
        thc2.Width = 70;
        thc2.BackColor = System.Drawing.Color.FromName("#9AC9F8");
        thr.Cells.Add(thc1);
        thr.Cells.Add(thc2);
        // table.Rows.Add(thr);
       
        for (int i = 0; i <= (pagopp.Tables[0].Rows.Count - 1); i++)
        {
            TableRow tr = new TableRow();
           
                if (i % 2 == 0)
                {
                    tr.BackColor = System.Drawing.Color.FromName("#CBE2FE");
                }
                int id =  int.Parse(pagopp.Tables["tablaPages"].Rows[i]["id_page"].ToString());
                TableCell tc1 = new TableCell();
                Label lb = new Label();
                lb.ID = "tb_" + id;
                lb.CssClass = "letraAzul14";
                lb.Text = pagopp.Tables["tablaPages"].Rows[i]["name_page"].ToString();
                tc1.Controls.Add(lb);
                tc1.Width = 180;
                TableCell tc2 = new TableCell();
                tc2.Text = "<a href= \"application.aspx?id=" + id + "\">Edit</a>";
                tc2.Width = 70;
                thc2.HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);

            table.Rows.Add(tr);
        }
        Panel1.Controls.Add(table);
    }
   
    protected void gv_application_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.CssClass = "letraNegra13";
            e.Row.Cells[0].Text = "Edit Application";
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[1].Style.Add("display", "none"); // Hide
            e.Row.Cells[3].Style.Add("display", "none"); // Hide
            e.Row.Cells[2].Text = "Application";
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Style.Add("display", "none"); // Hide
            e.Row.Cells[3].Style.Add("display", "none"); // Hide
            e.Row.CssClass = "letraNegra13";
        }
    }
 
    public void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
           // permiso = long.Parse(user.getRestricted().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 13);
            permiso = permi.int64C2;
        }
        if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
    }

    public void permiso()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            //permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 13);
            permiso = permi.int64C1;
        }
        if ((permiso & 1) != 1) { Panel1.Enabled = false; tb_application.Enabled = false; lnk_add.Enabled = false; }
    }


    protected void bt_addoption_Click(object sender, ImageClickEventArgs e)
    {}
    
    protected void lnk_add_Click(object sender, EventArgs e)
    {
        //primero obtengo el valor de permiso que toca
        Label1.Text = "";
        string sxml = "";
        sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <opcion><name>";
        sxml += tb_application.Text + "</name>";
        sxml += "<permiso>1</permiso>";
        sxml += "<app>1</app>";
        sxml += "</opcion>";
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        int pe = service.registraPage(sxml);
        if (pe == 1)
        {
            Label1.Text = "Page " + tb_application.Text + " added ";
            tb_application.Text = "";
            string op = service.getOpcionApp(1);
            pintarOpciones();
        }
    }
}